package com.sec.samsung.android.app.baymax.view.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.model.Question;
import com.sec.samsung.android.app.baymax.utils.StringUtility;

public class SingleChoiceAdapter extends BaseAdapter {

    private Context mContext;
    private OnCheckSingleItemListener listener;
    private ArrayList<String> mListAnswer = new ArrayList<String>();

    public SingleChoiceAdapter(Context context, Question question) {
        this.mContext = context;
        if (question != null)
            mListAnswer = getAnswers(question);
    }

    private ArrayList<String> getAnswers(Question question) {
        ArrayList<String> answer = new ArrayList<String>();
        if (!StringUtility.isEmpty(question.getAnswer1()) && question.getAnswer1() != null)
            answer.add(question.getAnswer1());
        if (!StringUtility.isEmpty(question.getAnswer2()) && question.getAnswer2() != null)
            answer.add(question.getAnswer2());
        if (!StringUtility.isEmpty(question.getAnswer3()) && question.getAnswer3() != null)
            answer.add(question.getAnswer3());
        if (!StringUtility.isEmpty(question.getAnswer4()) && question.getAnswer4() != null)
            answer.add(question.getAnswer4());
        if (!StringUtility.isEmpty(question.getAnswer5()) && question.getAnswer5() != null)
            answer.add(question.getAnswer5());
        if (!StringUtility.isEmpty(question.getAnswer6()) && question.getAnswer6() != null)
            answer.add(question.getAnswer6());
        return answer;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            convertView = (RelativeLayout) RelativeLayout.inflate(mContext, R.layout.single_answer_item, null);
        }

        RadioButton checkBox = (RadioButton) convertView.findViewById(R.id.radio_suggestion_symptoms);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                listener.onCheckItemSingleLisntener(position, isChecked);
            }
        });
        if (mListAnswer.size() > 0)
            checkBox.setText(mListAnswer.get(position));

        return convertView;
    }

    @Override
    public int getCount() {
        return mListAnswer.size();
    }

    @Override
    public String getItem(int position) {
        return mListAnswer.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface OnCheckSingleItemListener {
        public void onCheckItemSingleLisntener(int position, boolean value);
    }

    public void setLisntener(OnCheckSingleItemListener listener) {
        this.listener = listener;
    }

    public void setQuestion(Question question) {
        mListAnswer = getAnswers(question);
        notifyDataSetChanged();
    }
}